package com.lzb.musicplayer.dialog;

import android.content.Context;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;

import androidx.annotation.NonNull;

import com.blankj.utilcode.util.AppUtils;
import com.blankj.utilcode.util.TimeUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.lzb.basic.dialog.BaseSmartDialog;
import com.lzb.musicplayer.R;
import com.lzb.musicplayer.databinding.DialogSelTimeBinding;

import java.text.SimpleDateFormat;

import in.xiandan.countdowntimer.CountDownTimerSupport;
import in.xiandan.countdowntimer.OnCountDownTimerListener;


public class SelTimeDialog extends BaseSmartDialog<DialogSelTimeBinding> implements View.OnClickListener {

    private CountDownTimerSupport mTimer;

    public SelTimeDialog(@NonNull Context context) {
        super(context);
    }

    @Override
    protected boolean canceledOnTouchOutside() {
        return true;
    }

    @Override
    protected int getGravity() {
        return Gravity.CENTER;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.dialog_sel_time;
    }

    @Override
    protected void initView(View view) {
        mDataBinding.tvCancel.setOnClickListener(this);
        mDataBinding.tvConfirm.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.tvCancel) {
            dismiss();
        } else if (id == R.id.tvConfirm) {

            String hourStr = mDataBinding.etHour.getText().toString();
            String minuteStr = mDataBinding.etMinute.getText().toString();

            if (TextUtils.isEmpty(hourStr)) {
                hourStr = "00";
            }

            if (TextUtils.isEmpty(minuteStr)) {
                ToastUtils.showShort(getContext().getString(R.string.time_must_greater_zero));
                return;
            }

            if (hourStr.length() == 1) {
                hourStr = "0" + hourStr;
            }

            if (minuteStr.length() == 1) {
                minuteStr = "0" + minuteStr;
            }

            long time = TimeUtils.string2Millis(hourStr + ":" + minuteStr, new SimpleDateFormat("HH:mm"));
            if (time == 0) time = 1;
            if (mTimer == null) {
                mTimer = new CountDownTimerSupport(time, 1000);
            } else {
                mTimer.reset();
                mTimer = new CountDownTimerSupport(time, 1000);
            }
            mTimer.setOnCountDownTimerListener(new OnCountDownTimerListener() {
                @Override
                public void onTick(long millisUntilFinished) {

                }

                @Override
                public void onFinish() {
                    AppUtils.exitApp();
                }

                @Override
                public void onCancel() {
                    // 倒计时手动停止
                }
            });
            mTimer.start();
            dismiss();
            if (listener != null) {
                listener.clickConfirm();
            }
        }
    }

    private Ilistener listener;

    public void setListener(Ilistener listener) {
        this.listener = listener;
    }

    public interface Ilistener {
        void clickConfirm();
    }
}
